import "LotroLCD.LotroLCD.UI";

LotroLCDWindow = class(Turbine.UI.Window);
function LotroLCDWindow:Constructor(parent, settings, plugin)
	Turbine.UI.Lotro.Window.Constructor(self);
	
	-- Keep track of parent and base plugin object
	self.parent = parent;
	self.plugin = plugin;
	
	-- Associate settings
	self.settings = settings;
	
	-- Create a placeholder to use when window is minimized
	self.placeholder = LotroLCDPlaceholderWindow(self, self.settings, plugin);
	
	-- Set Look, feel, size  and position
	self:SetBackColorBlendMode(Turbine.UI.BlendMode.Normal);
	self:SetText(settings.windowTitle);
	self:SetPosition(100, 100);
	self:SetSize(165, 100);
	self:SetVisible(true);
	self:SetOpacity(settings.opacity);

	-- Detect double clicks on title to minimize/hide
	self.MouseDoubleClick = function( sender, args )
		self:SetVisible(false);
		self.placeholder:SetVisible(true);
	end
end

-- Create a config button
function LotroLCDWindow:ConstructConfigButton(self, window)
	self.configButton = Turbine.UI.Button();
	self.configButton:SetParent( self );
	self.configButton:SetPosition( 5, 25);
	self.configButton:SetBackground("LotroLCD/LotroLCD/Resources/LotroLCDSettings.tga");
	self.configButton:SetSize(20, 20);
	self.configButton:SetBlendMode(Turbine.UI.BlendMode.AlphaBlend)
	self.configButton.MouseClick = function( sender, args )
		if window:IsVisible() then
			window:SetVisible(false);
		else
			window:SetVisible(true);
		end
	end	

	-- On entering config button show different graphics
	self.configButton.MouseEnter = function( sender, args )
		self.configButton:SetBackground("LotroLCD/LotroLCD/Resources/LotroLCDSettingsMouseOver.tga");
	end

	-- On leaving config button show original graphics
	self.configButton.MouseLeave = function( sender, args )
		self.configButton:SetBackground("LotroLCD/LotroLCD/Resources/LotroLCDSettings.tga");
	end
end

-- Create a version label shown in lower right corner of "self" window
function LotroLCDWindow:ConstructVersionLabel(parent)
	local windowWidth, windowHeight = parent:GetSize();  
	self.version = Turbine.UI.Label();
	self.version:SetParent(self);
	self.version:SetPosition(windowWidth-102, windowHeight-52);
	self.version:SetSize(100, 50);
	self.version:SetFontStyle(Turbine.UI.FontStyle.Outline);
	self.version:SetFont(Turbine.UI.Lotro.Font.Verdana12);
	self.version:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleLeft);
	self.version:SetMouseVisible(false);
	local loaded_plugins = Turbine.PluginManager.GetAvailablePlugins()
	self.version:SetText(string.format("%s v%s",self.plugin.settings.name,self.plugin.settings.version));
end

-- Add push button to a listbox as required for all elements, resizing it and parent window as needed
function LotroLCDWindow:RefreshListbox(elements,listbox)
	--Add Labels as required
	for i,v in ipairs(elements) do
		if i > listbox:GetItemCount() then
			local width, height = listbox:GetSize();
			-- Create new button for each element
			local listItem = Turbine.UI.Label();
			
			-- Set Look, feel, size  and position
			listItem:SetSize( width, 20 );
			listItem:SetFontStyle(Turbine.UI.FontStyle.Outline);
			listItem:SetFont(Turbine.UI.Lotro.Font.Verdana14);
			listItem:SetTextAlignment(Turbine.UI.ContentAlignment.MiddleLeft);
			listItem:SetMultiline( false );
			listItem:SetZOrder(100);
			listItem:SetText(v);
			
			-- Add to window listbox and scale parent window
			listbox:AddItem( listItem );
			self:SetHeight(height + 70);
			listbox:SetHeight( height + 20);
		end
	end
	--Remove Labels as required
	local elementBalance = listbox:GetItemCount() - #elements ;
	for n = 1,elementBalance do
		local width, height = listbox:GetSize();
		listbox:RemoveItemAt(listbox:GetItemCount());
		self:SetHeight(height + 30);
		listbox:SetHeight( height - 20);
	end
end

-- Save settings for current window
function LotroLCDWindow:SaveSettings()
	self.settings.windowTitle = self:GetText();
	self.plugin:SaveSettings();
end